package com.ccb.testcenter.servicemaping;

import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ccb.testcenter.servicemapingconfig.DataContext;

/**
 * 
 * 类名：CustomRule 
 * 创建人：li_dk 
 * 修改人：li_dk 
 * 创建时间：2017年1月9日 上午10:53:39 
 * 修改时间：2017年1月9日上午10:53:39 
 * 文件版本：@version 1.0.0
 *
 */
public class CustomRule implements IConverter {
	private String id;
	private Class<?> clazz;
	private Method method;
	private LinkedList<String> params;
	
	public LinkedList<String> getParams() {
		return params;
	}

	public void setParams(LinkedList<String> params) {
		this.params = params;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Class<?> getClazz() {
		return clazz;
	}

	public void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}

	public Method getMethod() {
		return method;
	}

	public void setMethod(Method method) {
		this.method = method;
	}

	public String invoke(DataContext context) {
		String result = "";
		try {
			List<String> list = this.params;
			Object[] object = new Object[list.size()];
			Map<String, String> dataMap = context.getContextreq();
			for (int i = 0; i < list.size(); i++) {
				object[i] = dataMap.get(list.get(i));
			}
			result = (String) this.getMethod().invoke(this.getClazz().newInstance(), object);
			if (null == result) {
				return "";
			}
		} catch (Exception e) {
			Logger.getLogger("name").log(Level.WARNING, "类方法处理异常", e);
			return "";
		}
		return result;
	}

	@Override
	public String getConverterResult(DataContext context) {
		return invoke(context);
	}
}
